/* Copyright (c) 2007-2010 SmartVault Corporation, all rights reserved.
 * 
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestApp.h"
#include "TestDlg.h"
#include "TestToolbarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    m_appName.LoadStringW(IDS_APPNAME);
    m_section.LoadStringW(IDS_LASTCOUNT);
    CString tmp;
    tmp.GetEnvironmentVariableW(_T("TEMP"));
    m_configName.Format(_T("%s\\%s.ini"), tmp, m_appName);
}

CTestDlg::~CTestDlg()
{
    ListBox.Detach();
    OpenToolbar.Detach();
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
    ON_LBN_DBLCLK(IDC_LIST1, &CTestDlg::OnDoubleClick)
    ON_LBN_SELCHANGE(IDC_LIST1, &CTestDlg::OnSelectChange)
    ON_BN_CLICKED(IDC_BUTTON_NEW, &CTestDlg::OnNew)
    ON_BN_CLICKED(IDC_BUTTON_OPEN, &CTestDlg::OnOpenToolbar)
END_MESSAGE_MAP()


// CTestDlg message handlers

BOOL CTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    CenterWindow();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// Attach controls
    HWND hwnd = NULL;
    GetDlgItem(IDC_LIST1, &hwnd);
    ListBox.Attach(hwnd);
    
    hwnd = NULL;
    GetDlgItem(IDC_BUTTON_OPEN, &hwnd);
    OpenToolbar.Attach(hwnd);

    int count = ::GetPrivateProfileIntW(m_appName, m_section, 1, m_configName);
    for(int i = 0; i < count; i++ )
    {
        CString item;
        item.Format(IDS_CONTEXTFORMAT, i + 1);
        ListBox.AddString(item);
    }

    return TRUE;  // return TRUE  unless you set the focus to a control
}

HCURSOR CTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CTestDlg::OnNew()
{
    int nItems = ListBox.GetCount();

    CString value;
    value.Format(_T("%ld"), nItems + 1);

    ::WritePrivateProfileStringW(m_appName, m_section, value, m_configName);

    value.Format(IDS_CONTEXTFORMAT, nItems + 1);
    ListBox.AddString(value);
}

void CTestDlg::OnSelectChange()
{
    INT selIx = ListBox.GetCurSel();
    OpenToolbar.EnableWindow(selIx >= 0);
}

void CTestDlg::OnDoubleClick()
{
    if(OpenToolbar.IsWindowEnabled())
        OnOpenToolbar();
}

void CTestDlg::OnOpenToolbar()
{
    INT selIx = ListBox.GetCurSel();
    if(selIx >= 0)
    {
        CString ctxId;
        ctxId.Format(_T("Application Context ID - %ld"), 1 + selIx);
        //MessageBox(ctxId, _T("Selected"), MB_OK);

        TestToolbarDlg* pDlg = new TestToolbarDlg(ctxId, m_configName);
        pDlg->Create(TestToolbarDlg::IDD, this);
        pDlg->ShowWindow(SW_SHOW);
    }
}
